package cn.edu.zucc.online_market.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import cn.edu.zucc.online_market.itf.IaddressManager;
import cn.edu.zucc.online_market.model.BeanAddress;
import cn.edu.zucc.online_market.model.BeanUser;
import cn.edu.zucc.online_market.util.BaseException;
import cn.edu.zucc.online_market.util.BusinessException;
import cn.edu.zucc.online_market.util.DBPool;
import cn.edu.zucc.online_market.util.DbException;

public class AddressManager implements IaddressManager {

	//unfinished
	public BeanAddress loadByAddress_id(int address_id) throws BaseException{
		BeanAddress address = null;
		
		
		return address;
	}
	
	//finished
	
	
	public ArrayList<BeanAddress> loadAll() throws BaseException {
		// TODO Auto-generated method stub
		BeanUser user = BeanUser.currentLoginUser;
		Connection conn = null;
		BeanAddress p = null;
		ArrayList<BeanAddress> list_address = new ArrayList<BeanAddress>();
		try {
			conn = DBPool.getInstance().getConnection();
			String sql;
			java.sql.PreparedStatement pst;
			java.sql.ResultSet rs;
			sql = "select address_id,user_id,address_province,address_city,address_area,address_details,address_user_name,address_user_tele from shipping_address where user_id = ? ;";
			pst = conn.prepareStatement(sql);
			pst.setInt(1, user.getUser_id());
			rs = pst.executeQuery();
			while(rs.next()) {
				p = new BeanAddress();
				p.setAddress_id(rs.getInt(1));
				p.setUser_id(rs.getInt(2));
				p.setAddress_province(rs.getString(3));
				p.setAddress_city(rs.getString(4));
				p.setAddress_area(rs.getString(5));
				p.setAddress_details(rs.getString(6));
				p.setAddress_contact_name(rs.getString(7));
				p.setAddress_contact_telephone(rs.getString(8));
				list_address.add(p);
			}
			rs.close();
			pst.close();
		}catch(SQLException e) {
			e.printStackTrace();
			throw new DbException(e);
		}finally {
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return list_address;
	}
	
//	@Override
	public BeanAddress add(BeanUser user,String contact_user_name, String contact_telephone, String province, String city, String area,
			String address_details) throws BaseException{
		// TODO Auto-generated method stub
		BeanAddress address = new BeanAddress();
		Connection conn = null;
		try {
			
			conn = DBPool.getInstance().getConnection();
			conn.setAutoCommit(false);
			String sql;
			java.sql.PreparedStatement pst;
			java.sql.ResultSet rs;
			sql = "select max(address_id) from shipping address ;";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			int address_id = -1;
			if(rs.next()) {
				address_id = rs.getInt(1)+1;
			}else {
				address_id = 1;
			}
			
			sql = "insert into shipping_address  (user_id,address_province,address_city,address_area,address_details,address_user_name,address_user_tele,address_id) values(?,?,?,?,?,?,?,?);";
			pst = conn.prepareStatement(sql);
			pst.setInt(1, user.getUser_id());
			pst.setString(2, province);
			pst.setString(3, city);
			pst.setString(4, area);
			pst.setString(5, address_details);
			pst.setString(6, contact_user_name);
			pst.setString(7, contact_telephone);
			pst.setInt(8, address_id);
			pst.execute();
			pst.close();
			
			address.setAddress_id(address_id);
			address.setUser_id(user.getUser_id());
			address.setAddress_contact_name(contact_user_name);
			address.setAddress_contact_telephone(contact_telephone);
			address.setAddress_province(province);
			address.setAddress_city(city);
			address.setAddress_area(area);
			address.setAddress_details(address_details);
			
			conn.commit();
		}catch (SQLException e) {
			e.printStackTrace();
			throw new DbException(e);
		}finally {
			if(conn!=null)
				try {
//					conn.rollback();
					conn.close();
				}catch (SQLException e) {
					e.printStackTrace();
				}
		}
		
		
		return address;
	}

//	@Override
	public void delete(String telephone,int user_id) throws BaseException{
		// TODO Auto-generated method stub
		Connection conn = null;
		try {
			
			conn = DBPool.getInstance().getConnection();
			conn.setAutoCommit(false);
			String sql;
			java.sql.PreparedStatement pst;
			
			sql = "delete from shipping_address where address_user_telephone = ? and user_id = ?;";
			pst = conn.prepareStatement(sql);
			pst.setInt(2, user_id);
			pst.setString(1, telephone);
			pst.execute();
			conn.commit();
		}catch (SQLException e) {
			e.printStackTrace();
			throw new DbException(e);
		}finally {
			if(conn!=null)
				try {
					conn.close();
				}catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}
}
